// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

@use '../mixins';
@use '../variables';

.module-composition-input {
  &__quill {
    height: 100%;
    padding-inline: 12px;

    .ql-editor {
      caret-color: transparent;
      padding: 0;
      text-align: start;
      white-space: break-spaces;
      line-height: inherit;

      &--loaded {
        caret-color: auto;
      }

      &.ql-blank::before {
        inset-inline: 0;
        font-style: normal;
      }
    }
  }

  &__at-mention {
    background-color: variables.$color-gray-20;
    border-radius: 4px;
    display: inline;
    padding-inline: 4px;
    height: 22px;
    line-height: 22px;

    @include mixins.dark-theme {
      background-color: variables.$color-gray-60;
    }
  }

  &__input {
    $border-size: 1px;

    border-radius: 18px;
    overflow: hidden;
    word-break: break-word;

    // Override Quill styles
    .ql-container {
      // Inherit global font stack
      font-family: inherit;
      @include mixins.font-body-1;
    }

    .ql-blank::before {
      @include mixins.light-theme() {
        color: variables.$color-gray-45;
      }

      @include mixins.dark-theme() {
        color: variables.$color-gray-25;
      }
    }

    @include mixins.light-theme() {
      // Same as background color
      background-color: variables.$color-gray-05;
      color: variables.$color-gray-90;
    }

    @include mixins.dark-theme() {
      // Same as background color
      background-color: variables.$color-gray-75;
      color: variables.$color-gray-05;
    }

    &__scroller {
      $padding-top: 6px;
      padding-block: $padding-top;
      padding-inline: 0;

      min-height: calc(32px - 2 * $border-size);
      max-height: calc(72px - 2 * $border-size);
      overflow: auto;

      &--large {
        $largeHeight: calc(212px - 2 * $border-size);
        $largeHeightContentBox: calc($largeHeight - 2 * $padding-top);

        // Explicit height needed to allow `height: 100%` on children
        height: $largeHeight;
        max-height: $largeHeight;
        min-height: $largeHeight;

        .DraftEditor-root {
          height: $largeHeightContentBox;
        }

        .ql-editor {
          margin-inline-end: calc(-1 * variables.$scrollbar_width);
          padding-inline-end: variables.$scrollbar_width;
        }
      }

      &--link-preview {
        padding-top: 0;
      }
    }

    & {
      border: $border-size solid transparent;
    }

    &:focus-within {
      outline: 0;
      @include mixins.keyboard-mode {
        border: $border-size solid variables.$color-ultramarine;
      }
    }
  }

  // Note: This is referenced in ModalHost to ensure 'external' clicks on it still work
  &__format-menu {
    padding-block: 6px;
    padding-inline: 12px;
    border-radius: 8px;
    // This needs to be quite high to enable usage in popups like ForwardMessageModal
    z-index: variables.$z-index-above-popup;

    display: flex;
    flex-direction: row;

    opacity: 0;
    transition: opacity ease 200ms;

    // The same box-shadow in popper-shadow mixin, just halved
    box-shadow:
      0px 4px 10px rgba(0, 0, 0, 30%),
      0px 0px 4px rgba(0, 0, 0, 5%);

    @include mixins.light-theme() {
      background: variables.$color-white;
    }
    @include mixins.dark-theme() {
      background: variables.$color-gray-65;
    }

    &__item {
      $parent: &;
      @include mixins.button-reset;
      & {
        height: 24px;
        width: 24px;
        border-radius: 4px;
        margin-inline-end: 8px;
      }
      &:last-child {
        margin-inline-end: 0;
      }

      @include mixins.mouse-mode {
        &:hover {
          background-color: variables.$color-gray-05;
        }
      }
      @include mixins.dark-mouse-mode {
        &:hover {
          background-color: variables.$color-gray-60;
        }
      }

      &--active {
        @include mixins.light-theme {
          background-color: variables.$color-gray-05;
        }
        @include mixins.dark-theme {
          background-color: rgba(variables.$color-gray-45, 30%);
        }

        @include mixins.mouse-mode {
          &:hover {
            background-color: variables.$color-gray-15;
          }
        }
        @include mixins.dark-mouse-mode {
          &:hover {
            background-color: rgba(variables.$color-gray-45, 50%);
          }
        }
      }

      &__popover {
        @include mixins.font-subtitle-bold;
        & {
          padding-block: 5px;
          padding-inline: 8px;
          text-align: center;
          border-radius: 4px;
          margin-bottom: 8px;

          opacity: 0;
          transition: opacity 120ms ease-out;
        }

        @include mixins.light-theme {
          background-color: variables.$color-black;
          color: variables.$color-gray-05;
        }
        @include mixins.dark-theme {
          background-color: variables.$color-gray-65;
          color: variables.$color-gray-05;
        }

        &__shortcut {
          @include mixins.font-caption-bold;

          @include mixins.light-theme {
            color: variables.$color-gray-15;
          }
          @include mixins.dark-theme {
            color: variables.$color-gray-25;
          }
        }
      }

      &__icon {
        height: 20px;
        width: 20px;
        margin: 2px;

        &--bold {
          @include mixins.dark-theme {
            @include mixins.color-svg(
              '../images/icons/v3/text_format/textformat-bold-bold.svg',
              variables.$color-gray-25
            );
          }
          @include mixins.light-theme {
            @include mixins.color-svg(
              '../images/icons/v3/text_format/textformat-bold-bold.svg',
              variables.$color-gray-60
            );
          }
        }

        &--italic {
          @include mixins.dark-theme {
            @include mixins.color-svg(
              '../images/icons/v3/text_format/textformat-italic-bold.svg',
              variables.$color-gray-25
            );
          }
          @include mixins.light-theme {
            @include mixins.color-svg(
              '../images/icons/v3/text_format/textformat-italic-bold.svg',
              variables.$color-gray-60
            );
          }
        }

        &--strike {
          @include mixins.dark-theme {
            @include mixins.color-svg(
              '../images/icons/v3/text_format/textformat-strikethrough-bold.svg',
              variables.$color-gray-25
            );
          }
          @include mixins.light-theme {
            @include mixins.color-svg(
              '../images/icons/v3/text_format/textformat-strikethrough-bold.svg',
              variables.$color-gray-60
            );
          }
        }

        &--monospace {
          @include mixins.dark-theme {
            @include mixins.color-svg(
              '../images/icons/v3/text_format/textformat-monospace-bold.svg',
              variables.$color-gray-25
            );
          }
          @include mixins.light-theme {
            @include mixins.color-svg(
              '../images/icons/v3/text_format/textformat-monospace-bold.svg',
              variables.$color-gray-60
            );
          }
        }

        &--spoiler {
          @include mixins.dark-theme {
            @include mixins.color-svg(
              '../images/icons/v3/text_format/textformat-spoiler-bold.svg',
              variables.$color-gray-25
            );
          }
          @include mixins.light-theme {
            @include mixins.color-svg(
              '../images/icons/v3/text_format/textformat-spoiler-bold.svg',
              variables.$color-gray-60
            );
          }
        }

        // Here we look at hover for the parent so the 2px border is a hover target
        // Note: We can't use the mixins because .mouse-mode would end up after the >
        .mouse-mode #{$parent}:hover & {
          background-color: variables.$color-gray-90;
        }
        .dark-theme.mouse-mode #{$parent}:hover & {
          background-color: variables.$color-gray-15;
        }

        &--active {
          @include mixins.dark-theme {
            background-color: variables.$color-ultramarine-light;
          }
          @include mixins.light-theme {
            background-color: variables.$color-ultramarine;
          }

          // Override above hover behaviors
          .mouse-mode #{$parent}:hover & {
            background-color: variables.$color-ultramarine;
          }
          .dark-theme.mouse-mode #{$parent}:hover & {
            background-color: variables.$color-ultramarine-light;
          }
        }
      }
    }
  }

  // Note: This is referenced in ModalHost to ensure 'external' clicks on it still work
  &__suggestions {
    padding: 0;
    margin-bottom: 6px;
    border-radius: 8px;
    z-index: variables.$z-index-above-popup;
    overflow: hidden;

    &--scroller {
      max-height: 300px;
      overflow-y: auto;
    }

    @include mixins.popper-shadow();

    @include mixins.light-theme() {
      background: variables.$color-white;
    }

    @include mixins.dark-theme() {
      background: variables.$color-gray-75;
    }

    &__row {
      height: 34px;
      padding-block: 0;
      padding-inline: 12px;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: flex-start;
      background: none;
      border: none;
      width: 100%;

      &--mention {
        height: 40px;
      }

      &:focus {
        outline: 0;
      }

      & {
        @include mixins.font-body-2;
      }

      @include mixins.light-theme() {
        color: variables.$color-gray-60;
      }

      @include mixins.dark-theme() {
        color: variables.$color-gray-25;
      }

      &__short-name {
        margin-inline-start: 4px;
      }

      &--selected,
      &:hover {
        @include mixins.light-theme() {
          background: variables.$color-gray-05;
          color: variables.$color-gray-90;
        }

        @include mixins.dark-theme() {
          background: variables.$color-gray-60;
          color: variables.$color-gray-05;
        }
      }
    }

    &__title {
      padding-inline-start: 8px;
    }

    & {
      stroke: variables.$color-white;
    }
  }

  &__editing-message {
    @include mixins.font-body-2-bold;
    margin-top: 10px;
    user-select: none;

    &::before {
      content: '';
      display: inline-block;
      height: 16px;
      margin-block: 0;
      margin-inline: 10px 8px;
      width: 16px;
      vertical-align: middle;

      @include mixins.color-svg(
        '../images/icons/v3/edit/edit.svg',
        variables.$color-black
      );

      @include mixins.dark-theme {
        @include mixins.color-svg(
          '../images/icons/v3/edit/edit.svg',
          variables.$color-gray-15
        );
      }
    }

    &__attachment img {
      border-radius: 4px;
      height: 18px;
      position: absolute;
      inset-inline-end: 8px;
      top: 8px;
      width: 18px;
    }
  }
}

div.CompositionInput__link-preview {
  background: variables.$color-gray-02;
  border-radius: 8px;
  flex-direction: row-reverse;
  justify-content: space-between;
  margin: 6px;
  min-height: 74px;
  overflow: hidden;

  @include mixins.dark-theme {
    background: variables.$color-gray-80;
  }

  &__icon-container {
    align-items: center;
    display: flex;
    margin-inline: 8px 0;
  }

  &__content {
    margin-inline-end: 0;
    padding-bottom: 8px;
    padding-inline-start: 12px;
    padding-top: 8px;
  }

  &__no-image {
    margin-inline-end: 0;
    min-width: 74px;
  }
}

button.CompositionInput__link-preview__close-button {
  -webkit-mask: none;
  @include mixins.rounded-corners;
  align-items: center;
  backdrop-filter: blur(12px);
  background: rgba(variables.$color-gray-05, 0.8);
  display: flex;
  height: 20px;
  justify-content: center;
  inset-inline-end: 6px;
  top: 6px;
  width: 20px;

  @include mixins.dark-theme {
    background-color: rgba(variables.$color-gray-75, 0.8);
    -webkit-mask: none;
  }

  &::before {
    @include mixins.color-svg(
      '../images/icons/v3/x/x.svg',
      variables.$color-black-alpha-50
    );

    @include mixins.dark-theme {
      background-color: variables.$color-white-alpha-55;
    }

    & {
      content: '';
      height: 16px;
      width: 16px;
    }
  }
}

// Note: These are referenced in formatting/matchers.ts, to detect these styles on paste
.quill {
  &--monospace {
    font-family: variables.$monospace;
  }
  &--spoiler {
    @include mixins.light-theme {
      // vs color/$color-gray-90, background/$color-gray-05
      background-color: variables.$color-gray-25;
    }
    @include mixins.dark-theme {
      // vs color/$color-gray-05, background/$color-gray-95
      background-color: variables.$color-gray-45;
    }
  }
}
